import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
// const originalPush = Router.prototype.push
// Router.prototype.push = function push(location, onResolve, onReject) {
//   if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
//   return originalPush.call(this, location).catch(err => err)
// }
const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/index',
      component: () => import('@/views/layouts/index'),
      redirect: '/home',
      children: [
        {
          path: '/home',
          name: 'home',
          component: () => import('@/views/home/index'),
          meta: {
            title: '工作台',
            keepAlive: false
          }
        }
      ]
    },
    {
      path: '/student',
      name: 'student',
      component: () => import('@/views/student/index'),
      meta: {
        title: '学生列表',
        keepAlive: false
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/index'),
      keepAlive: false
    }
  ],
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return {
        x: 0,
        y: 0
      }
    }
  }
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // if (localStorage.routes) {
  //   store.dispatch('setRoutes', localStorage.routes)
  // router = window.localStorage.getItem('routes')
  // console.log(routes)
  // if (localStorage.getItem("routes")) {
  //   store.dispatch('setRoutes', localStorage.getItem("routes"))
  // }

  //  console.log(localStorage.getItem("routes"))
  //  document.body.scrollTop = 0;
  //  // firefox
  //  document.documentElement.scrollTop = 0;
  //  // safari
  //  window.pageYOffset = 0;
  const isLogin = localStorage.token ? true : false
  const isLoading = localStorage.getItem('userType')
  if (to.path !== '/login' || to.path !== '/loading') {
    next()
  } else {
    isLogin ? next() : next('/login')
  }
})

export default router
